﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MyBlog.Common
{
   public class CookieHelper
    {
       public static HttpCookie GetCookie(string cookieName)
       {
           if (HttpContext.Current.Request.Cookies.AllKeys.Contains(cookieName))
           {
               return HttpContext.Current.Request.Cookies[cookieName];
           }
           return null;
       }

       public static string GetCookieValue(string cookieName, string key)
       {
           var cookie = GetCookie(cookieName);
           if (cookie != null)
           {
               if (cookie.Values[key] != null)
               {
                   return cookie.Values[key];
               }
           }
           return null;
       }

       public static void SetCookie(string cookieName, string value)
       {
           SetCookie(cookieName, null, value, null);
       }

       public static void SetCookie(string cookieName, string value, DateTime? expires)
       {
           SetCookie(cookieName, null, value, expires);
       }

       public static void SetCookie(string cookieName, string key, string value)
       {
           SetCookie(cookieName, key, value, null);
       }

       public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
       {
           var cookie = GetCookie(cookieName);
           if (cookie != null)
           {
               if (expires!=null)
               {
                   cookie.Expires = expires.Value;
               }
               if (!string.IsNullOrEmpty(key)&&cookie.HasKeys && cookie.Values.AllKeys.Contains(key))
               {
                   cookie.Values[key] = value;
               }
           }
       }

       public static void AddCookie(HttpCookie cookie)
       {
           if (cookie!=null)
           {
               cookie.HttpOnly = true;
               //cookie.Path = "/";
               HttpContext.Current.Response.Cookies.Add(cookie);
           }
       } 

       public static void AddCookie(string cookieName,string value)
       {
           AddCookie(cookieName, null, value, null);
       }

       public static void AddCookie(string cookieName, string value, DateTime expires)
       {
           AddCookie(cookieName, null, value, expires);
       }

       public static void AddCookie(string cookieName, string key, string value, DateTime? expires)
       {
           var cookie = new HttpCookie(cookieName);
           if (expires != null)
           {
               cookie.Expires = expires.Value;
           }
           if (!string.IsNullOrEmpty(key))
           {
               cookie.Values.Add(key, value);
            }
            else
            {
                cookie.Value = value;
            }
           AddCookie(cookie);
       }

       public static void RemoveCookie(string cookieName, string key)
       {
           var oldCookie = HttpContext.Current.Request.Cookies[cookieName];
           if (oldCookie != null)
           {
               if (!string.IsNullOrEmpty(key) && oldCookie.HasKeys && oldCookie.Values.AllKeys.Contains(key))
               {
                   oldCookie.Values.Remove(key);
               }
               var cookie = new HttpCookie(cookieName);
               cookie.Value = null;
               cookie.Expires = DateTime.Now.AddDays(-1);
               HttpContext.Current.Response.Cookies.Add(cookie);
           }
       }

       public static void RemoveCookie(string cookieName)
       {
           RemoveCookie(cookieName, null);
       }



    }
}
